import random

class pin(object) :
    """This object contains all the methods to build pins based on MACS and BITS
    attributes: bit, bits, macs, parent"""

    def __init__(self, bit=None, bits=10, macs=7, parent=None) :
        if bit == None and parent == None :
            self.bits = bits
            self.macs = macs
            self.randompin()
        elif bit == None and parent != None :
            self.bits = bits
            self.macs = macs
            self.parent = parent
            self.nextpin()

    def randompin(self) :
        self.bit = random.randint(0, (self.bits - 1))

    def nextpin(self) :
        self.randompin()
        while abs(self.bit - self.parent) > self.macs :
            self.randompin()

    def __str__(self) :
        return str(self.bit)
        

class key(object) :
    """defines the structure of a key, as well as methods for generating key with
    different levels
    attributes: code, pins, level, parent, bits, macs"""
    def __init__(self, code=None, bits=10, macs=7, pins=6, parent=None, level=0) :
        if parent == None and code == None and level == 0 :
            self.bits = bits
            self.pins = pins
            self.macs = macs
            self.randomkey()
        """elif len(self.parent) == self.pins and self.level != 0 :
            if self.level == 1 :

            elif self.level == 2 :

            elif self.level == 3 :

            elif self.level == 4 :

            elif self.level == 5 :
        """ 
    def randomkey(self) :
        rdmkey = []
        count = int()
        while count != (self.pins) :
            if count < 1 :
                rdmkey.append(pin(bits=self.bits))
            else :
                rdmkey.append(pin(bits=self.bits, parent=rdmkey[-1].bit))
                                
            if count >= 1 :
                num1 = rdmkey[len(rdmkey) - 2].bit
                num2 = rdmkey[len(rdmkey) - 3].bit
                if num1 == num2 :
                    pretest = num1
                else : 
                    pretest = 1000
                while rdmkey[-1] == pretest :
                    rdmkey[-1] = pin(bits=self.bits)
            count += 1
        self.code = rdmkey

    def __str__(self) :
        string = str()
        for n in self.code :
            string += str(n)
        return string

class keyset(object) :
    """This class details a set of keys of a particular level
    attributes: level, keys, parent, bits, macs"""
    def __init__(self, level=0, keys=20, bits=10, macs=6) :
        
key1 = key(pins=10)
print key1
            
"""            
def pin(type):
  "this function generates a random pin # valid to the type (total number of pin code possibilities)"
  a = 0
  b = type - 1
  return random.randint(a,b)

def randomkey(pins, bits, macs) :
    bits = int(bits)
    rdmkey = []
    count = int()
    while count != (pins) :
        rdmkey.append(pin(bits))
        if count >= 1 :
            num1 = rdmkey[len(rdmkey) - 2]
            num2 = rdmkey[len(rdmkey) - 3]
            if num1 == num2 :
                pretest = num1
            else : 
                pretest = 1000
            while abs(rdmkey[len(rdmkey) - 2] - rdmkey[-1]) > macs or rdmkey[-1] == pretest :
                rdmkey[-1] = pin(bits)
        count += 1
    return rdmkey

def lint(list) :
    "this function takes a list of strings and turns it into a list of integers"
    test = []
    if type(list) != type(test) :
        return list
    list = [int(n) for n in list]
    return list

def lstr(list) :
    "this function turns a list of integers into a list of strings"
    test = []
    if type(list) != type(test) :
        return list
    list = [str(n) for n in list]
    return list

def subkey(code, bits, macs, level) :
    "this function generates the next key in a 2 step progression schema"
    code = list(code)
    code = lint(code)
    if level == 1 :
        code[2] = abs(code[2] + 2) % bits
        if code[2] <= 1 :
            code[3] = abs(code[3] + 2) % bits
        code = lstr(code)
        code = ''.join(code)
    #elif level == 2 :
        
        
    return code

#def keyset(code, bits, macs, level) :
    #if level ==

def validator(code, bits, macs, level) :
    test = int()
    if level == 0 :
        code = ''.join(code)
        return code
    elif type(level) != type(test) or level >= 5 :
        level_error = 'please enter valid level, 1-5'
        return level_error
    
    

#print randomkey(6, 10, 6)
print subkey('05007', 10, 6, 7)        
    
"""
